class Solution {
    
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) 
    {
        int temp = 0, sum = 0, index = 0;
        
        for (int i = 0; i < gas.size(); ++i) 
        {
            temp += gas[i]-cost[i];
            sum += gas[i]-cost[i];
            
            if (temp < 0) 
            {
                index = i+1;
                temp = 0;
            }
        }
        
        return sum>=0 ? index : -1;
    }
};